using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace SOFTECH.Components
{
    public partial class Register
    {
        #region Private members.

        protected int _ID;
        protected string _CompanyCode = string.Empty;
        protected string _CompanyName = string.Empty;
        protected string _CompanyAddress = string.Empty;
        protected string _CompanyPhone = string.Empty;
        protected string _EmployeeEmail = string.Empty;
        protected string _EmployeeFullName = string.Empty;
        protected string _EmployeeID = string.Empty;
        protected string _EmployeePhone = string.Empty;
        protected string _EmployeeTitle = string.Empty;
        protected string _Product = string.Empty;
        protected DateTime _CreatedDate;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public string CompanyCode
        {
            set { this._CompanyCode = value; }
            get { return this._CompanyCode; }
        }

        public string CompanyName
        {
            set { this._CompanyName = value; }
            get { return this._CompanyName; }
        }

        public string CompanyAddress
        {
            set { this._CompanyAddress = value; }
            get { return this._CompanyAddress; }
        }

        public string CompanyPhone
        {
            set { this._CompanyPhone = value; }
            get { return this._CompanyPhone; }
        }

        public string EmployeeEmail
        {
            set { this._EmployeeEmail = value; }
            get { return this._EmployeeEmail; }
        }

        public string EmployeeFullName
        {
            set { this._EmployeeFullName = value; }
            get { return this._EmployeeFullName; }
        }

        public string EmployeeID
        {
            set { this._EmployeeID = value; }
            get { return this._EmployeeID; }
        }

        public string EmployeePhone
        {
            set { this._EmployeePhone = value; }
            get { return this._EmployeePhone; }
        }

        public string EmployeeTitle
        {
            set { this._EmployeeTitle = value; }
            get { return this._EmployeeTitle; }
        }

        public string Product
        {
            set { this._Product = value; }
            get { return this._Product; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Register Load(int iD)
        {
            const string spName = "[dbo].[p_SOFTECH_Register_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Register entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Register();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyCode"))) entity.CompanyCode = reader.GetString(reader.GetOrdinal("CompanyCode"));
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyName"))) entity.CompanyName = reader.GetString(reader.GetOrdinal("CompanyName"));
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyAddress"))) entity.CompanyAddress = reader.GetString(reader.GetOrdinal("CompanyAddress"));
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyPhone"))) entity.CompanyPhone = reader.GetString(reader.GetOrdinal("CompanyPhone"));
                if (!reader.IsDBNull(reader.GetOrdinal("EmployeeEmail"))) entity.EmployeeEmail = reader.GetString(reader.GetOrdinal("EmployeeEmail"));
                if (!reader.IsDBNull(reader.GetOrdinal("EmployeeFullName"))) entity.EmployeeFullName = reader.GetString(reader.GetOrdinal("EmployeeFullName"));
                if (!reader.IsDBNull(reader.GetOrdinal("EmployeeID"))) entity.EmployeeID = reader.GetString(reader.GetOrdinal("EmployeeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("EmployeePhone"))) entity.EmployeePhone = reader.GetString(reader.GetOrdinal("EmployeePhone"));
                if (!reader.IsDBNull(reader.GetOrdinal("EmployeeTitle"))) entity.EmployeeTitle = reader.GetString(reader.GetOrdinal("EmployeeTitle"));
                if (!reader.IsDBNull(reader.GetOrdinal("Product"))) entity.Product = reader.GetString(reader.GetOrdinal("Product"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------


        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_SOFTECH_Register_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_SOFTECH_Register_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_SOFTECH_Register_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_SOFTECH_Register_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(string companyCode, string companyName, string companyAddress, string companyPhone, string employeeEmail, string employeeFullName, string employeeID, string employeePhone, string employeeTitle, string product)
        {
            Register entity = new Register();
            entity.CompanyCode = companyCode;
            entity.CompanyName = companyName;
            entity.CompanyAddress = companyAddress;
            entity.CompanyPhone = companyPhone;
            entity.EmployeeEmail = employeeEmail;
            entity.EmployeeFullName = employeeFullName;
            entity.EmployeeID = employeeID;
            entity.EmployeePhone = employeePhone;
            entity.EmployeeTitle = employeeTitle;
            entity.Product = product;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_SOFTECH_Register_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@CompanyCode", SqlDbType.NVarChar, this._CompanyCode);
            db.AddInParameter(dbCommand, "@CompanyName", SqlDbType.NVarChar, this._CompanyName);
            db.AddInParameter(dbCommand, "@CompanyAddress", SqlDbType.NVarChar, this._CompanyAddress);
            db.AddInParameter(dbCommand, "@CompanyPhone", SqlDbType.VarChar, this._CompanyPhone);
            db.AddInParameter(dbCommand, "@EmployeeEmail", SqlDbType.VarChar, this._EmployeeEmail);
            db.AddInParameter(dbCommand, "@EmployeeFullName", SqlDbType.NVarChar, this._EmployeeFullName);
            db.AddInParameter(dbCommand, "@EmployeeID", SqlDbType.VarChar, this._EmployeeID);
            db.AddInParameter(dbCommand, "@EmployeePhone", SqlDbType.VarChar, this._EmployeePhone);
            db.AddInParameter(dbCommand, "@EmployeeTitle", SqlDbType.NVarChar, this._EmployeeTitle);
            db.AddInParameter(dbCommand, "@Product", SqlDbType.NVarChar, this._Product);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, string companyCode, string companyName, string companyAddress, string companyPhone, string employeeEmail, string employeeFullName, string employeeID, string employeePhone, string employeeTitle, string product)
        {
            Register entity = new Register();
            entity.ID = iD;
            entity.CompanyCode = companyCode;
            entity.CompanyName = companyName;
            entity.CompanyAddress = companyAddress;
            entity.CompanyPhone = companyPhone;
            entity.EmployeeEmail = employeeEmail;
            entity.EmployeeFullName = employeeFullName;
            entity.EmployeeID = employeeID;
            entity.EmployeePhone = employeePhone;
            entity.EmployeeTitle = employeeTitle;
            entity.Product = product;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_SOFTECH_Register_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@CompanyCode", SqlDbType.NVarChar, this._CompanyCode);
            db.AddInParameter(dbCommand, "@CompanyName", SqlDbType.NVarChar, this._CompanyName);
            db.AddInParameter(dbCommand, "@CompanyAddress", SqlDbType.NVarChar, this._CompanyAddress);
            db.AddInParameter(dbCommand, "@CompanyPhone", SqlDbType.VarChar, this._CompanyPhone);
            db.AddInParameter(dbCommand, "@EmployeeEmail", SqlDbType.VarChar, this._EmployeeEmail);
            db.AddInParameter(dbCommand, "@EmployeeFullName", SqlDbType.NVarChar, this._EmployeeFullName);
            db.AddInParameter(dbCommand, "@EmployeeID", SqlDbType.VarChar, this._EmployeeID);
            db.AddInParameter(dbCommand, "@EmployeePhone", SqlDbType.VarChar, this._EmployeePhone);
            db.AddInParameter(dbCommand, "@EmployeeTitle", SqlDbType.NVarChar, this._EmployeeTitle);
            db.AddInParameter(dbCommand, "@Product", SqlDbType.NVarChar, this._Product);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Register entity = new Register();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_SOFTECH_Register_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion
    }
}